package com.zd.commodity.productMng.action;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONObject;
import com.zd.commodity.action.BaseAction;
import com.zd.commodity.productMng.biz.IProductIntroductionBiz;
import com.zd.commodity.productMng.entity.ProductIntroduction;
import com.zd.commodity.userMng.entity.User;
@Scope("prototype")
@Controller
public class ProductIntroductionAction extends BaseAction<ProductIntroduction>{
	
	private IProductIntroductionBiz productIntroductionBiz;
	private ProductIntroduction productIntroduction;
	
	//用txt文件上传详情
	public void txtIntroductionHandle() throws InterruptedException{
		User user = (User)request.getSession().getAttribute("user");
		String path = request.getServletContext().getRealPath("");
		String message = productIntroductionBiz.updateByTxts(user,path);
		write(message);
	}
	
	//修改商品详情
	public void updateProductIntroduction(){
		User user = (User)request.getSession().getAttribute("user");
		ProductIntroduction introduction = productIntroductionBiz.get(productIntroduction.getId());
		introduction.setUpdater(user);
		introduction.setName(productIntroduction.getName());
		introduction.setContent(productIntroduction.getContent());
		introduction.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		try {
			productIntroductionBiz.update(introduction);
			write(ajaxReturn(true,"修改成功！",null));
		} catch (Exception e) {
			write(ajaxReturn(false,"修改失败！",null));
			e.printStackTrace();
		}
	}
	//添加商品详情
	public void addProductIntroduction(){
		User user = (User)request.getSession().getAttribute("user");
		productIntroduction.setUpdater(user);
		productIntroduction.setCreator(user);
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		productIntroduction.setUpdateTime(timestamp);
		productIntroduction.setCreationTime(timestamp);
		try {
			productIntroductionBiz.add(productIntroduction);
			write(ajaxReturn(true,"添加成功！",null));
		} catch (Exception e) {
			write(ajaxReturn(false,"添加失败！",null));
			e.printStackTrace();
		}
	}
	

	//添加商品或修改商品时需要显示详情信息
	public void showProductIntroduction(){
		ProductIntroduction introduction=productIntroductionBiz.get(super.getId());
		JSONObject json=new JSONObject();
		json.put("name", introduction.getName());
		json.put("content", introduction.getContent());
		write(json.toJSONString());
	}
		

	public IProductIntroductionBiz getProductIntroductionBiz() {
		return productIntroductionBiz;
	}
	@Autowired
	public void setProductIntroductionBiz(
			IProductIntroductionBiz productIntroductionBiz) {
		this.productIntroductionBiz = productIntroductionBiz;
		this.setBaseBiz(productIntroductionBiz);
	}
	public ProductIntroduction getProductIntroduction() {
		return productIntroduction;
	}
	public void setProductIntroduction(ProductIntroduction productIntroduction) {
		this.productIntroduction = productIntroduction;
	}
	
}
